package com.caesar.Net.UDP;

/**
 * @author caesar
 * @date 2025/7/22
 * @Description:
 * @apiNote
 */
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Receive {
    public static void main(String[] args) throws Exception{
        DatagramSocket ds = new DatagramSocket(8888);//告诉网卡驱动，我要准备与网络进行通信
        //告诉网络驱动，一会儿有给8888端口号的消息，你给我传过来

        byte[] data = new byte[1024];
        DatagramPacket dp = new DatagramPacket(data,data.length);
        //一会儿接收的数据会被放到data数组中

        //接收数据
        ds.receive(dp);

        //拆解数据
        int len = dp.getLength();//获取实际接收了几个字节
        String str = new String(data,0, len);
        System.out.println("接收的消息：" + str);

        ds.close();
    }
}
